const global = getApp().globalData
const topicApi = global.topicApi
const wxApi = global.wxApi;
Component({
  data: {
    unsubedTopicViewShow: false,
    topicHeaderHeight: 130,
    inputModalShow: false,
    recordShow: false,
    showGuidance: false,
    screenHeight: 0,
    topicCanStudy: true
  },
  properties: {
    topic: {
      type: Object,
      observer: '__topicObserver'
    }
  },
  ready() {
    this.guidanceImp();
    this.viewHeightCalc();
    // 设置可视区域高度
    wxApi.getScreenSize( size => {
      this.setData({
        screenHeight: size.height,
        unsubedTopicViewShow: true
      })
    })
  },
  methods: {
    __topicObserver(val) {
      console.log('接收到新的知识点信息')
      console.log(val);
      this.setData({
        topicCanStudy: this.topicCanStudy(val)
      })
    },
    topicCanStudy(topic) {
      let subed = JSON.parse(topic.textbookSubscribed);
      let free = JSON.parse(topic.free);
      return free || subed
    },
    viewHeightCalc() {
      let that = this;
      let timer = setTimeout(() => {
        var query = wx.createSelectorQuery();
        query.select('#topic-header').boundingClientRect()
        query.exec(function (res) {
          if (!res[0]) return;
          //取高度
          that.setData({
            topicHeaderHeight: res[0].height
          })
          console.log(res[0].height);
        })
        clearTimeout(timer);
        timer = null;
      }, 300)
    },
    /**
     * 单词拼写
     */
    writeTopic() {
      console.log('show modal')
      this.setData({
        inputModalShow: true
      })
    },
    // 拼写知识点确认事件
    writeTopicSure(e) {
      let msg = '';
      if (this.data.topic.content === e.detail) {
        msg = '拼写正确'
      } else {
        msg = '拼写错误'
      }
      wx.showToast({
        title: msg,
        icon: 'none'
      })
    },
    /**
     * 打开录音弹窗
     *
     */
    onStartRecord() {
      this.setData({
        recordShow: true
      })
    },
    /**
     * 收藏单词
     */
    onTopicCollect() {
      let topic = this.data.topic;
      if ( topic ) {
        if ( !topic.favorite ) {
          topicApi.postCollectTopic(topic.id, (res, code) => {
            this.setData({
              'topic.favorite': true
            })
          })
        } else {
          topicApi.deleteCollectTopic(topic.id, (res, code) => {
            this.setData({
              'topic.favorite': false
            })
          })
        }
      }
    },
    onAssociateClick(e) {
      let topic = e.detail;
      let params = {
        topic: wxApi.encodeURI(topic)
      }

      let path = 'topic-detail';
      wxApi.navigateTo(path, params);
    },
    // 选中关键字
    onTagTap(e) {
      this.triggerEvent('tagTap', e.detail);
    },
    // 关闭引导
    onTapGuidance() {
      this.setData({
        showGuidance: false
      })
    },
    /**
     * 引导图处理逻辑
     */
    guidanceImp() {
      global.store.getGuidTimeByKey('dcxx', num => {
        console.log( num );
        let showGuidance = false;
        if ( num === 1 ) {
          showGuidance = true
        } else {
          showGuidance = false
        }
        this.setData({
          showGuidance: showGuidance
        })
      })
    },
    // 模态窗口状态更新
    onUpdateAttr: function (e) {
      this.setData(e.detail);
    }
  }
})